package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.OmsCompanyAddress;
import com.itheima.domain.OmsOrderReturnApply;
import com.itheima.mapper.OmsCompanyAddressMapper;
import com.itheima.mapper.OmsOrderReturnApplyMapper;
import com.itheima.service.db.OmsOrderReturnApplyService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@DubboService
public class OmsOrderReturnApplyServiceImpl implements OmsOrderReturnApplyService {
    @Autowired
    OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;
    @Autowired
    OmsCompanyAddressMapper omsCompanyAddressMapper;
    // 条件查询
    @Override
    public PageBeanVo getList(Integer pageNum, Integer pageSize, String createTime, String handleMan, String handleTime, Integer id, String receiverKeyword, Integer status) {
        Page<OmsOrderReturnApply> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper=new LambdaQueryWrapper<>();
        // 申请时间 查询   这里createTime时Dade类型,数据库也是Date类型所有不需要转型
        queryWrapper.eq(StrUtil.isNotEmpty(createTime),OmsOrderReturnApply::getCreateTime,createTime);
        queryWrapper.eq(StrUtil.isNotEmpty(handleMan),OmsOrderReturnApply::getHandleMan,handleMan);
        queryWrapper.eq(StrUtil.isNotEmpty(handleTime),OmsOrderReturnApply::getHandleTime,handleTime);
        queryWrapper.eq(id!=null,OmsOrderReturnApply::getId,id);
        queryWrapper.eq(status!=null,OmsOrderReturnApply::getStatus,status);
        Page<OmsOrderReturnApply> page1 = omsOrderReturnApplyMapper.selectPage(page, queryWrapper);

        return new PageBeanVo(pageNum,pageSize,page1.getTotal(),page1.getRecords());
    }
    // 批量删除
    @Override
    public void delete(List<Integer> ids) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderReturnApply::getId,ids);
        omsOrderReturnApplyMapper.delete(queryWrapper);
    }
    // 退货详情
    @Override
    public OmsOrderReturnApply particulars(Integer id) {
        return omsOrderReturnApplyMapper.selectById(id);
    }
    // 地址列表
    @Override
    public List<OmsCompanyAddress> companyAddressList() {
        LambdaQueryWrapper<OmsCompanyAddress> queryWrapper=new LambdaQueryWrapper<>();
        List<OmsCompanyAddress> omsCompanyAddresses = omsCompanyAddressMapper.selectList(queryWrapper);
        return omsCompanyAddresses;
    }

    @Override
    public void updateStatus(Integer id, OmsOrderReturnApply omsOrderReturnApply) {
        OmsOrderReturnApply getOms = omsOrderReturnApplyMapper.selectById(id);
        BeanUtil.copyProperties(omsOrderReturnApply,getOms,getNullPropertyNames(omsOrderReturnApply));
        System.out.println(getOms.toString());
        omsOrderReturnApplyMapper.updateById(getOms);

    }
    // 解决BeanUtil.copyProperties拷贝同种对象,null也拷贝问题
    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

}
